package com.tmt.im.wallet.service;

import com.tmt.im.common.enums.IMAuthorityEnum;
import com.tmt.im.common.pojo.*;
import com.tmt.im.wallet.helper.IgniteQueryHelper;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.util.CollectionUtils;

import java.security.Principal;
import java.text.MessageFormat;
import java.util.List;

/**
 * @Description TODO
 * @Author wuyi
 * @Date 2024/8/12 16:02
 * @Version 1.0
 **/
public class BaseService {

    protected final Ignite ignite;

    protected final IgniteCache<Long, RedPackageSkinPoJo> skinCache;

    protected final IgniteCache<IMFriendKey, IMFriendPoJo> friendCache;

    protected final IgniteCache<Long, IMUserPoJo> userCache;

    protected final IgniteCache<Long, WalletPoJo> walletCache;

    protected final IgniteCache<Long, WalletChangeDetailPoJo> walletDetailCache;

    protected final IgniteCache<BlackBillKey, BlackBillPoJo> blackBillCache;

    protected final IgniteCache<Long, ChatSessionPoJo> sessionCache;

    protected final IgniteCache<SessionMemberKey, ChatSessionMemberPoJo> sessionMemberCache;

    protected final IgniteCache<Long, RedPackagePoJo> redPackageCache;

    protected final IgniteCache<RedPackageDetailKey, RedPackageDetailPoJo> redPackageDetailCache;

    protected final IgniteCache<Long, RechargeHistoryPoJo> rechargeHistoryCache;


    protected final IgniteCache<Long, WithdrawHistoryPoJo> withdrawHistoryCache;

    protected final IgniteCache<Long, WithdrawBindingParamPoJo> withdrawParamCache;

    public BaseService(Ignite ignite) {
        this.ignite = ignite;
        userCache = ignite.cache(IMUserPoJo.CACHE_NAME);
        walletCache = ignite.cache(WalletPoJo.CACHE_NAME);
        skinCache = ignite.cache(RedPackageSkinPoJo.CACHE_NAME);
        friendCache = ignite.cache(IMFriendPoJo.CACHE_NAME);
        blackBillCache = ignite.cache(BlackBillPoJo.CACHE_NAME);
        walletDetailCache = ignite.cache(WalletChangeDetailPoJo.CACHE_NAME);
        sessionCache = ignite.cache(ChatSessionPoJo.CACHE_NAME);
        sessionMemberCache = ignite.cache(ChatSessionMemberPoJo.CACHE_NAME);
        redPackageCache = ignite.cache(RedPackagePoJo.CACHE_NAME);
        redPackageDetailCache = ignite.cache(RedPackageDetailPoJo.CACHE_NAME);
        rechargeHistoryCache = ignite.cache(RechargeHistoryPoJo.CACHE_NAME);
        withdrawHistoryCache = ignite.cache(WithdrawHistoryPoJo.CACHE_NAME);
        withdrawParamCache = ignite.cache(WithdrawBindingParamPoJo.CACHE_NAME);
    }

    public ChatSessionMemberPoJo getSessionMemberBy(Long currentUserId, Long sessionId) {
        String sql = MessageFormat.format("select * from {0} where sessionId = ? and memberId = ?", ChatSessionMemberPoJo.CACHE_NAME);
        return IgniteQueryHelper.findOneBy(sql, new Object[]{sessionId, currentUserId},
                ChatSessionMemberPoJo.class, sessionMemberCache);
    }

    /**
     * 是否有足够权限。
     *
     * @param principal
     * @param authorityEnums
     * @return
     */
    public boolean hasAuthenticated(Principal principal, List<IMAuthorityEnum> authorityEnums) {
        if (principal instanceof JwtAuthenticationToken token) {
            //查询是否具备权限
            List<String> authorities = token.getToken().getClaim("authorities");
            return !CollectionUtils.isEmpty(authorityEnums)
                    && authorityEnums.stream().anyMatch(x -> authorities.contains(x.name()));
        }
        return false;
    }

}
